{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#https://www.runoob.com/python/python-gui-tkinter.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，大树\n",
      "你好，大树\n",
      "你好，大叔\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in Tkinter callback\n",
      "Traceback (most recent call last):\n",
      "  File \"E:\\software\\Python\\anaconda\\lib\\tkinter\\__init__.py\", line 1705, in __call__\n",
      "    return self.func(*args)\n",
      "  File \"<ipython-input-2-d8ef62da354e>\", line 75, in submit\n",
      "    result = self.fanyi.crawl(content)\n",
      "  File \"<ipython-input-2-d8ef62da354e>\", line 22, in crawl\n",
      "    answer = json.loads(r.text)\n",
      "  File \"E:\\software\\Python\\anaconda\\lib\\json\\__init__.py\", line 348, in loads\n",
      "    return _default_decoder.decode(s)\n",
      "  File \"E:\\software\\Python\\anaconda\\lib\\json\\decoder.py\", line 337, in decode\n",
      "    obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n",
      "  File \"E:\\software\\Python\\anaconda\\lib\\json\\decoder.py\", line 355, in raw_decode\n",
      "    raise JSONDecodeError(\"Expecting value\", s, err.value) from None\n",
      "json.decoder.JSONDecodeError: Expecting value: line 1 column 27 (char 26)\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "from tkinter import Tk,Button,Entry,Label,Text,END\n",
    "\n",
    "class YouDaoFanyi(object):\n",
    "    def __init__(self):\n",
    "        pass\n",
    "    def crawl(self,word):\n",
    "        url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'\n",
    "        data = {\n",
    "            'i': word,\n",
    "            'from': 'AUTO',\n",
    "            'to': 'AUTO',\n",
    "            'smartresult': 'dict',\n",
    "            'client': 'fanyideskweb',\n",
    "            'doctype': 'json',\n",
    "            'version': '2.1',\n",
    "            'keyfrom': 'fanyi.web',\n",
    "            'action': 'FY_BY_REALTlME'\n",
    "        }\n",
    "        r = requests.post(url,data)\n",
    "        answer = json.loads(r.text)\n",
    "        result = answer['translateResult'][0][0]['tgt']\n",
    "        return result\n",
    "\n",
    "class Application(object):\n",
    "    def __init__(self):\n",
    "        self.window = Tk()\n",
    "        self.fanyi = YouDaoFanyi()\n",
    "    \n",
    "        #设置窗口大小和位置\n",
    "        self.window.title(u'我的翻译')\n",
    "        self.window.geometry('310x370+500+300')\n",
    "        self.window.minsize(310,370)\n",
    "        self.window.maxsize(310,370)\n",
    "    \n",
    "        #创建一个文本框\n",
    "        #self.entry = Entry(self.window)\n",
    "        #self.entry.place(x=10,y=10,width=200,height=25)\n",
    "        #self.entry.bind(\"<Key-Return>\",self.submit1)\n",
    "        self.result_text1 = Text(self.window,background = 'azure')\n",
    "        # 喜欢什么背景色就在这里面找哦，但是有色差，得多试试：\n",
    "        # http://www.science.smith.edu/dftwiki/index.php/Color_Charts_for_TKinter\n",
    "        self.result_text1.place(x = 10,y = 5,width = 285,height = 155)\n",
    "        self.result_text1.bind(\"<Key-Return>\",self.submit1)\n",
    "\n",
    "        #创建一个按钮\n",
    "        #为按钮添加事件\n",
    "        self.submit_btn = Button(self.window,text=u'翻译',command=self.submit)\n",
    "        self.submit_btn.place(x=205,y=165,width=35,height=25)\n",
    "        self.submit_btn2 = Button(self.window,text=u'清空',command = self.clean)\n",
    "        self.submit_btn2.place(x=250,y=165,width=35,height=25)\n",
    "        \n",
    "        #翻译结果标题\n",
    "        self.title_label = Label(self.window,text=u'翻译结果:')\n",
    "        self.title_label.place(x=10,y=165)\n",
    "        \n",
    "        #翻译结果\n",
    "        self.result_text = Text(self.window,background = 'light cyan')\n",
    "        self.result_text.place(x = 10,y = 190,width = 285,height = 165)\n",
    "        \n",
    "    def submit1(self,event):\n",
    "        #从输入框获取用户输入的值\n",
    "        content = self.result_text1.get(0.0,END).strip().replace(\"\\n\",\" \")\n",
    "        #把这个值传送给服务器进行翻译\n",
    "        result = self.fanyi.crawl(content)\n",
    "        #将结果显示在窗口中的文本框中\n",
    "        self.result_text.delete(0.0,END)\n",
    "        self.result_text.insert(END,result)\n",
    "    \n",
    "    def submit(self):\n",
    "        #从输入框获取用户输入的值\n",
    "        content = self.result_text1.get(0.0,END).strip().replace(\"\\n\",\" \")\n",
    "        #把这个值传送给服务器进行翻译\n",
    "        result = self.fanyi.crawl(content)\n",
    "        #将结果显示在窗口中的文本框中\n",
    "        self.result_text.delete(0.0,END)\n",
    "        self.result_text.insert(END,result)\n",
    "        print(content)\n",
    "        \n",
    "    def clean(self):\n",
    "        self.result_text1.delete(0.0,END)\n",
    "        self.result_text.delete(0.0,END)\n",
    "        \n",
    "    def run(self):\n",
    "        self.window.mainloop()\n",
    "        \n",
    "if __name__=='__main__':\n",
    "    app = Application()\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
